package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"wechat/pkg"
)

func GetToken() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.GetHeader("token")
		if token != "" {
			c.JSONP(http.StatusBadRequest, gin.H{
				"code": 400,
				"msg":  "token不能为空",
			})
			c.Abort()
			return
		}

		handler, err := pkg.TokenHandler(token)
		if err != nil {
			c.JSONP(http.StatusBadRequest, gin.H{
				"code": 400,
				"msg":  "token解析失败",
			})
			return
		}
		c.Set("userId", handler)
		c.Next()

	}
}
